package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.query.TravelCommendQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IRedisService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.domain.DestinationTemplate;
import cn.wolfcode.luowowo.search.domain.StrategyTemplate;
import cn.wolfcode.luowowo.search.domain.TravelTemplate;
import cn.wolfcode.luowowo.search.domain.UserInfoTemplate;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import cn.wolfcode.luowowo.website.web.annotation.CheckLoginAnnotation;
import cn.wolfcode.luowowo.website.web.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
public class IndexController {
    @Reference
    private IRedisService redisService;
    @Reference
    private ITravelCommendService travelCommendService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationSearchService destinationSearchService;
    @Reference
    private ITravelSearchService travelSearchService;
    @Reference
    private IUserInfoSearchService userInfoSearchService;
    @Reference
    private IStrategySearchService strategySearchService;
    @Reference
    private ISearchService searchService;
    @RequestMapping("")
    public String indexList(HttpServletRequest req, HttpServletResponse resp, Model model){
        String token = CookieUtil.getToken(req,resp);
        UserInfo userInfo = redisService.getValueByToken(token);
        model.addAttribute("userInfo",userInfo);

        //tcs 游记推荐
        model.addAttribute("tcs",travelCommendService.getTtravelCommendTop5());
        //sc 攻略推荐第一
        model.addAttribute("sc",strategyCommendService.getStrategryTop1());
        return "index/index";
    }

    @RequestMapping("index/travelPage")
    public String travelPage(TravelQuery qo, Model model){
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "index/travelPageTpl";
    }

    @RequestMapping("q")
    public String search(@ModelAttribute("qo") SearchQueryObject qo, Model model){
        switch (qo.getType()){
            case SearchQueryObject.SEARCH_TYPE_DEST:
                return searchByDest(qo,model);
            case SearchQueryObject.SEARCH_TYPE_STRATEGY:
                return searchByStrategy(qo,model);
            case SearchQueryObject.SEARCH_TYPE_TRAVEL:
                return searchByTravel(qo,model);
            case SearchQueryObject.SEARCH_TYPE_USER:
                return searchByUserInfo(qo,model);
                default:
                    return searchAll(qo,model);
        }
    }

    private String searchByDest(SearchQueryObject qo, Model model){
        DestinationTemplate dest = destinationSearchService.findDestByName(qo.getKeyword());
        SearchResultVO vo = new SearchResultVO();
        if(dest!=null){
            List<StrategyTemplate> sts = strategySearchService.findStrategysByDestName(qo.getKeyword());
            if(sts.size() > 5){
                sts = sts.subList(0,5);
            }
            List<TravelTemplate> tts = travelSearchService.findTravelsByDestName(qo.getKeyword());
            if(tts.size() > 5){
                tts = tts.subList(0,5);
            }

            List<UserInfoTemplate> uts = userInfoSearchService.findUserInfosByDestName(qo.getKeyword());
            if(uts.size() > 5){
                uts = uts.subList(0,5);
            }
            vo.setStrategys(sts);
            vo.setTravels(tts);
            vo.setUsers(uts);
            vo.setTotal(vo.getTotal()+sts.size()+ tts.size()+uts.size());
            model.addAttribute("data",vo);
            model.addAttribute("dest",dest);
        }
        return "index/searchDest";
    }
    private String searchByStrategy(SearchQueryObject qo, Model model){
        Page<StrategyTemplate> page = searchService.searchWithHighlight("luowowo_strategy", "strategy", StrategyTemplate.class, qo, "title", "subTitle", "summary");
        model.addAttribute("page",page);
        return "index/searchStrategy";
    }
    private String searchByTravel(SearchQueryObject qo, Model model){
        Page<TravelTemplate> page = searchService.searchWithHighlight("luowowo_travel", "travel", TravelTemplate.class, qo, "title", "summary");
        model.addAttribute("page",page);
        return "index/searchTravel";
    }
    private String searchByUserInfo(SearchQueryObject qo, Model model){
        Page<UserInfoTemplate> page = searchService.searchWithHighlight("luowowo_userinfo", "userinfo", UserInfoTemplate.class, qo, "nickName", "destName");
        model.addAttribute("page",page);
        return "index/searchUser";
    }
    private String searchAll(SearchQueryObject qo, Model model){
        SearchResultVO vo = new SearchResultVO();
        Page<DestinationTemplate> pdt = searchService.searchWithHighlight("luowowo_destination", "destination", DestinationTemplate.class, qo, "name", "info");
        Page<StrategyTemplate> pst = searchService.searchWithHighlight("luowowo_strategy", "strategy", StrategyTemplate.class, qo, "title", "subTitle", "summary");
        Page<TravelTemplate> ptt = searchService.searchWithHighlight("luowowo_travel", "travel", TravelTemplate.class, qo, "title", "summary");
        Page<UserInfoTemplate> put = searchService.searchWithHighlight("luowowo_userinfo", "userinfo", UserInfoTemplate.class, qo, "nickName", "destName");
        vo.setDests(pdt.getContent());
        vo.setStrategys(pst.getContent());
        vo.setTravels(ptt.getContent());
        vo.setUsers(put.getContent());
        vo.setTotal(pdt.getTotalElements()+pst.getTotalElements()+ptt.getTotalElements()+put.getTotalElements());
        model.addAttribute("data",vo);
        return "index/searchAll";
    }
}

